/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.*;

/**
 *
 * @author plasalmonie
 */
public class Control extends HttpServlet {

    private Pays p;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page;
        if (request.getParameter("choix") == null) {
            //appel de la JSP
            page = "accueil.jsp";
        } else if (request.getParameter("choix").equals("dep")) {
            String choixDep = request.getParameter("choixDep");
            if (choixDep == null) {
                request.setAttribute("listeDep", p.getLesDep());
                page = "liste_departement.jsp";
            } else {
                Dep d = p.getLeDep(choixDep);
                request.setAttribute("listeNom", d.getLesMeds());
                page = "liste_nom.jsp";
            }

        } else if (request.getParameter("choix").equals("nom")) {
            String choixNom = request.getParameter("choixNom");
            if (choixNom == null) {
                page = "choix_nom.jsp";
            } else {
                Collection<Med> maCol = new TreeSet<Med>();
                for (Dep d : p.getLesDep()) {
                    for (Med m : d.getLesMeds()) {
                        if (m.getNom().startsWith(choixNom)) {
                            maCol.add(m);
                        }
                    }
                }
                request.setAttribute("listeNom", maCol);
                page = "liste_nom.jsp";
            }


        } else if (request.getParameter("choix").equals("spe")) {
            String choixSpe = request.getParameter("choixSpe");
            if (choixSpe == null) {
                Collection<Spe> spe = p.getLesSpes();
                page = "choix_spe.jsp";
                request.setAttribute("listeSpe", spe);
            } else {
                Spe s = p.getLaSpe(choixSpe);
                request.setAttribute("listeNom", s.getLesMeds());
                page = "liste_nom.jsp";
            }


        }else {
            page = "accueil.jsp";
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    public void destroy() {
        Connect.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}